#include <stdio.h>  
#include <stdlib.h>  
  
typedef struct data 
{  
    int T;  // 温度  
    int H;  // 湿度  
} data;  
    
typedef struct {  
    data *elements; // 指向动态分配数组的指针  
    size_t capacity; // 顺序表的容量  
    size_t length;   // 顺序表当前存储的元素数量  
} SeqList;  
  
// 初始化顺序表  
SeqList* createSeqList(size_t initialCapacity) 
{  
    SeqList *list = (SeqList*)malloc(sizeof(SeqList));  
    if (!list) 
    {
        return NULL;  
    }
    list->elements = (data*)calloc(initialCapacity, sizeof(data));  
    if (!list->elements) 
    {  
        free(list);  
        return NULL;  
    }  
    list->capacity = initialCapacity;  
    list->length = 0;  
    return list;  
}  
  
// 向顺序表中添加元素  
int addElement(SeqList *list, int T, int H) 
{  
    if (list->length >= list->capacity) 
    {  
          
        size_t newCapacity = list->capacity * 2;  
        data *newElements = (data*)realloc(list->elements, newCapacity * sizeof(data));  
        if (!newElements) 
        {
            return -1; 
        }  
        list->elements = newElements;  
        list->capacity = newCapacity;  
    }  
    list->elements[list->length].T = T;  
    list->elements[list->length].H = H;  
    list->length++;  
    return 0;  
}  
  
// 打印顺序表  
void printSeqList(SeqList *list) 
{  
    for (size_t i = 0; i < list->length; i++) 
    {  
        printf("D1[%zu] T %d D1[%zu] H %d\n", i, list->elements[i].T, i, list->elements[i].H);  
    }  
}  
  
// 释放顺序表占用的内存  
void freeSeqList(SeqList *list) 
{  
    free(list->elements);  
    free(list);  
}  
  
int main(int argc, char const *argv[]) 
{  
    SeqList *list = createSeqList(10); // 初始容量为10  
    if (!list) 
    {  
        printf("创建顺序表失败\n");  
        return -1;  
    }  
  
    // 向顺序表中添加元素  
    addElement(list, 25, 60);  
    addElement(list, 30, 70);  
    addElement(list, 35, 80);  
  
    // 打印顺序表  
    printSeqList(list);  
  
    // 释放内存  
    freeSeqList(list);  
  
    return 0;  
}